package com.innovatio.validator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This code is property of Innovatio Software Solutions, Inc.
 * User: Ramon
 * Modified by: Edxe
 * Date: Mar 4, 2009
 * Time: 11:49:49 AM
 */
public class ValidationErrors implements Serializable {

    private Map<String, List<Error>> errorList = new HashMap<String, List<Error>>();


    /**
     * appends the errors form a ValidationError Object to this instance.
     */
    public void appendErros(ValidationErrors errors){
        this.errorList.putAll(errors.errorList);
    }

    /**
     * returns whethe the bean has error on the validation or not
     */
    public boolean hasErrors() {
        return errorList.size() > 0;
    }

    /**
     * after the validation checks if a particular fields pass the validation
     *
     * @param fieldName bean field name
     */
    public boolean fieldContainError(String fieldName) {
        return errorList.containsKey(fieldName);
    }

    /**
     * after the validation gets the error if any for a particular field
     *
     * @param fieldName bean field name
     * @return returns null if there's no error with the field
     */
    public List<Error> getFieldErrors(String fieldName) {
        return errorList.containsKey(fieldName) ? errorList.get(fieldName) : null;
    }

    public List<Error> getErrors() {
        List<Error> le = new ArrayList<Error>();
        for (Map.Entry<String, List<Error>> e : this.errorList.entrySet()) {
            le.addAll(e.getValue());
        }
        return le;
    }

    /**
     * Adds an validation error to the list of errors founded for the bean.
     *
     * @param fieldName the field name from the bean
     * @param error     the error object descriptor
     */
    public void addValidationError(String fieldName, Error error) {
        List<Error> errors;
        if (errorList.containsKey(fieldName))
            errors = errorList.get(fieldName);
        else
            errors = new ArrayList<Error>();
        errors.add(error);
        errorList.put(fieldName, errors);
    }

    /**
     * Adds an validation error to the list of errors founded for the bean.
     *
     * @param fieldName    the field name from the bean
     * @param errorMessage the error message
     */
    public void addValidationError(String fieldName, String errorMessage) {
        addValidationError(fieldName, new Error(fieldName, errorMessage));
    }

    /**
     *
     */
    public static class Error {
        private String fieldName;
        private String errorMessage;

        public Error(String field, String errorMessage) {
            this.fieldName = field;
            this.errorMessage = errorMessage;
        }

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getErrorMessage() {
            return errorMessage;
        }

        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }
    }
}
